package com.origin.floatwindowpermission.permission;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.core.view.ViewCompat;

import java.lang.ref.WeakReference;

public class InAppFloatWindowManager {
    private static volatile InAppFloatWindowManager instance;
    private FrameLayout.LayoutParams mParams = null;

    public static InAppFloatWindowManager getInstance() {
        if (instance == null) {
            synchronized (InAppFloatWindowManager.class) {
                if (instance == null) {
                    instance = new InAppFloatWindowManager();
                }
            }
        }
        return instance;
    }

    private FrameLayout getActivityRoot(Activity activity) {
        if (activity == null) {
            return null;
        }
        try {
            return (FrameLayout) activity.getWindow().getDecorView().findViewById(android.R.id.content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void attach(Activity activity) {
        ensureFloatingView();
        attach(getActivityRoot(activity));
    }

    public boolean isInAppFloat() {
        return mContainer != null;
    }

    public void detach(Activity activity) {
        detach(getActivityRoot(activity));
    }

    public void detach(FrameLayout container) {
        if (mEnFloatingView != null && container != null && ViewCompat.isAttachedToWindow(mEnFloatingView)) {
            container.removeView(mEnFloatingView);
        }
        if (getContainer() == container) {
            mContainer = null;
        }
    }
    private FrameLayout getContainer() {
        if (mContainer == null) {
            return null;
        }
        return mContainer.get();
    }

    private InAppFloatView mEnFloatingView;

    private void ensureFloatingView() {
        synchronized (this) {
            if (mEnFloatingView != null) {
                return;
            }
            InAppFloatView avCallFloatView = new InAppFloatView(EnContext.get());
            mEnFloatingView = avCallFloatView;
            mParams = new FrameLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            mParams.gravity = Gravity.RIGHT | Gravity.CENTER;
            avCallFloatView.setParams(mParams);
        }
    }


    private WeakReference<FrameLayout> mContainer;

    private void attach(FrameLayout container) {
        if (container == null || mEnFloatingView == null) {
            mContainer = new WeakReference<>(container);
            return;
        }
        if (mEnFloatingView.getParent() == container) {
            return;
        }
        if (mEnFloatingView.getParent() != null) {
            ((ViewGroup) mEnFloatingView.getParent()).removeView(mEnFloatingView);
        }
        mContainer = new WeakReference<>(container);
        container.addView(mEnFloatingView);
    }


}
